﻿/* 
| - - Motion.Euzo.Ca - - - Motion and M+ from euzo.ca - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
*/

/* 
This file will search for the custom html tag “HtmlEditor” and will convert it to an html editor, will wire its events, will 
apply the necessary styles, and will display the html inside. 
*/

// Create the motion namespace if no one else has created it yet.
if (!Motion)
{
    var Motion = {};
}

(function ()
{
    // Create the framework namespace if it was not created yet.
    if (!Motion.Framework)
    {
        Motion.Framework = {};
    }
})();


(function ()
{
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *    
    Motion.Framework.HtmlEditor = {};
    var HtmlEditor = Motion.Framework.HtmlEditor // Shortcut to the HtmlEditor namespace.


    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *    
    // Once the document is loaded, look for all HtmlEditors and assigns them with the required functions, the HtmlEditors are html 
    // tags that are styled by CSS and functioning by Script.
    Motion.Framework.HtmlEditor.OnLoad = function ()
    {
        window.removeEventListener('load', HtmlEditor.OnLoad, false);

        var HtmlEditorTags = document.all.tags("HtmlEditor");
        if (HtmlEditorTags != null)
        {        
            for (HtmlEditorTagsIndex = 0; HtmlEditorTagsIndex < HtmlEditorTags.length; HtmlEditorTagsIndex++)
            {
                var HtmlEditorTag = HtmlEditorTags[HtmlEditorTagsIndex];
                HtmlContentPath = HtmlEditorTag.getAttribute('content');
                if (HtmlContentPath)
                {
                    var httpLoad = new XMLHttpRequest();
                    httpLoad.open('GET', HtmlContentPath);
                    httpLoad.onreadystatechange = HtmlEditor.OnContentLoaded;
                    httpLoad.HtmlEditorTag = HtmlEditorTag;
                    httpLoad.send();                  
                }
            }
        }
    }


    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *    
    // Display the content in the editor once it is loaded.
    Motion.Framework.HtmlEditor.OnContentLoaded = function(loadProgress)
    {
        if(loadProgress.target.status != 200)
        {
            alert('Unable to load the html file');
            return;
        }
        if(loadProgress.target.readyState != 4)
        {
            // If the document is not loaded return.
            return;
        }
        
        HtmlEditorTag = loadProgress.target.HtmlEditorTag;
        loadProgress.target.HtmlEditorTag = null;
        HtmlEditorTag.innerText = loadProgress.target.responseText;
    }

})();


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *    
// Wait until the entire page is loaded with all external scripts and files, then initialize the page.
window.addEventListener('load', Motion.Framework.HtmlEditor.OnLoad, false);
